package com.hhit.bigscreenbackmanage.service.impl.jsgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.dao.jsgl.TJsglSwcgyyMapper;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglStrwjd;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglSwsjyy;
import com.hhit.bigscreenbackmanage.service.jsgl.TJsglSwcgyyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TJsglSwcgyyServiceImpl implements TJsglSwcgyyService {

    private Logger log = LoggerFactory.getLogger(TJsglSwcgyyServiceImpl.class);

    @Autowired
    TJsglSwcgyyMapper tJsglSwcgyyMapper;

    @Override
    public Map<String, Integer> querySwcgjfCount(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))){
            throw new Exception("缺少必要参数: dataTime");
        }

        Map<String, Integer> result = new HashMap<>();

        try{
            result = tJsglSwcgyyMapper.querySwcgjfCount(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }

        if(result.get("cbsj") == null){
            result.put("cbsj",0);
        }
        if(result.get("sgt") == null){
            result.put("sgt",0);
        }
        if(result.get("jgt") == null){
            result.put("jgt",0);
        }
        return result;
    }

    @Override
    public Map<String, Object> querySwsjyyList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSwcgyyMapper.querySwsjyyNum(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());

        List<TJsglSwsjyy> list = new ArrayList<>();
        try{
            list = tJsglSwcgyyMapper.querySwsjyyList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }

        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public void deleteSwsjyyRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }

        try {
            tJsglSwcgyyMapper.deleteSwsjyyRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }
}
